package com.qf.controller;

import com.qf.pojo.req.SellerReq;
import com.qf.pojo.resp.BaseResp;
import com.qf.service.SellerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @ClassName SellerController
 * @Description TODO
 * @Author 非乐
 * @Date 2022/10/4 14:58
 * @Version 1.0
 **/
@RestController
@RequestMapping("seller")
@Api(tags = "管理员相关接口")
public class SellerController {

    @Autowired
    SellerService sellerService;

    @PostMapping("register")
    @ApiOperation("注册管理员(用户名，密码，邮箱，验证码必传递)")
    public BaseResp register(@RequestBody SellerReq sellerReq, HttpSession session){
        return sellerService.register(sellerReq,session);
    }

    @PostMapping("sendEmail")
    @ApiOperation("发送验证码(邮箱必传)")
    public BaseResp sendEmail(@RequestBody SellerReq sellerReq,HttpSession session){
        return sellerService.sendEmail(sellerReq.getEmail(),session);
    }

    @PostMapping("login")
    @ApiOperation("管理员登录")
    public BaseResp login(@RequestBody SellerReq sellerReq){
        return sellerService.login(sellerReq);
    }

    @GetMapping("logout")
    @ApiOperation("退出登录")
    public BaseResp logout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return new BaseResp(200,"success",null);
    }

    @GetMapping("isAuthcation")
    @ApiOperation("判断是否登录")
    public BaseResp isAuthcation(){
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()){
            return new BaseResp(200,"success",subject.getPrincipal());
        }else {
            return new BaseResp().ERROR();
        }
    }
}
